package com.xyzh05.dao.hibernate;

import java.util.List;

import com.xyzh05.model.Department;
import com.xyzh05.dao.DepartmentDao;
import com.xyzh05.dao.hibernate.GenericDaoHibernate;
import org.springframework.stereotype.Repository;

@Repository("departmentDao")
public class DepartmentDaoHibernate extends GenericDaoHibernate<Department, Long> implements DepartmentDao {

    public DepartmentDaoHibernate() {
        super(Department.class);
    }
    
    @SuppressWarnings("unchecked")
	public List<Department> getDepartmentByName(String deptName){
    	String sql = "from Department dept where dept.id != 1 and dept.displayName like '%" + deptName + "%'";
    	return getSession().createQuery(sql).list();
    }
    
    @SuppressWarnings("unchecked")
	public List<Department> getDepartmentById(Long deptId){
    	String sql = "select * from department d where 1=1";
    	if(deptId != null && !"".equals(deptId)){
    		sql += " and d.id=" + deptId + " or d.parent_id=" + deptId;
    	}
    	return getSession().createSQLQuery(sql).addEntity(Department.class).list();
    }
}
